var url = "http://127.0.0.1:8083/";

function getCookie(key){
    var cookie = document.cookie;
    var s = cookie.replace("; ","=");
    while (s.indexOf(";") != -1){
        s = s.replace("; ","=");
    }
    var strings = s.replace("; ","=").split("=");
    if (strings.indexOf(key) == -1){
        return null;
    }
    return strings[strings.indexOf(key) + 1].trim();
}
function toPage(pageName) {
    $.ajax({
        url:url + "user/"+pageName,
        type:"GET",
        async:false,
        success:function (data) {
            if (data["code"] == 200) {
                window.cookie="data" + data["data"];
                window.location.href=url + "user" + data["data"]+".html";
            }else if (data["code"] == 204) {
                window.location.href=url + "user" + data["data"]+".html";
            }
        }
    })
}

function write(obj,newListElement){
    var product = $(obj).find(".single-product-item");
    let a_image = product.find(".single-product-img").find("a");
    var price = product.find(".new-price");
    var name = product.find("h3").find("a");
    var modal = product.find(".modal-view");

    a_image.attr("data-target", newListElement["productId"]);
    product.find(".primary-image").attr("src",newListElement["productPicture"]);
    price.text("￥" + newListElement["productPrice"]);
    name.html(newListElement["productName"]);
    modal.attr("title",newListElement["productId"]);
}

//商品详情页 - 商品详情显示
$(function () {
    let id = getCookie("PRODUCT");

    if (isNaN(id)){
        id = -1;
    }
    $.ajax({
        url:url + "user/showDetails",
        type:"POST",
        data:{productId:id},
        success:function (data) {
            if(data["code"] == 200){
                var product = data["data"]["product"];

                $("#zoom_01").attr("src",product["productPicture"]);

                var $product = $(".product-detail");

                $product.find(".product-title").find("h2").html(product["productName"]);
                $product.find(".product-title").find("h4").html("￥" + product["productPrice"]);
                $product.find(".remain").html(product["inventoryRemain"]);
                $(".tab-content #description").html(data["data"]["productInfo"]["productInfoIntroduction"]);

                $(".product-detail-area .wish-icon-product-details .pid").val(product["productId"]);
                showRelatedList(product["productId"]);
            }
        }

    })
});

//相关商品列表显示
function showRelatedList(productId) {
    $.ajax({
        url: url + "user/relatedList",
        type: "POST",
        data:{productId: productId},
        success:function (data) {
            if (data["code"] == 200) {
                let relatedList = data["data"];
                let i = 0;
                $(".tab-content .related-product-wrapper div.col-xs-12").each(function () {
                    if (i < relatedList.length){
                        write(this,relatedList[i++]);
                    }
                })
            }
        }
    })
}

//添加到购物车
$("a.add-to-cart").click(function () {
    var id = $(this).parent().parent().find(".pid");
    var $count = $(".product-detail .size-quantity .pro-quantity");
    $.ajax({
        url: url + "cart/addCart",
        type: "POST",
        data: {
            userName: getCookie("USER_NAME"),
            productId: id.val(),
            count: $count.val(),
        },
        success: function (data) {
            if (data["code"] == 200) {
                window.location.reload();
            }else if(data["code"] == 405){
                window.location.href=url + "user/user_loginUI.html";
            }
        }
    });
});

//显示评论
$(function () {
    var productId = $(".product-detail-area input.pid").val();
    $.ajax({
        url: url + "comments/getCommentsList",
        type: "POST",
        data:{productId: getCookie("PRODUCT")},
        success:  function (data) {
            if (data["code"] == 200){
                var datum = data["data"];
                var li = "";
                for (let i = 0; i < datum.length; i++) {
                    var $ul = $(".product-detail-area .product-description-tab #review ul");
                    var user = datum[i]["user"];
                    li += "<li>\n" +
                        "    <p class=\"user\">\n" +
                        "        <i class=\"fa fa-circle\"></i>\n" +
                        "        <span class=\"name\">" + user["userName"] + "</span>\n" +
                        "        <span class=\"time\">" + datum[i]["commentsTime"] + "</span>\n" +
                        "    </p>\n" +
                        "    <p>" + datum[i]["commentsContent"] +
                        "    </p>" +
                        "</li>"
                }
                $ul.html(li);
            }
        }
    })
});
